<template>
	<view class="perEdit">
		<view class="header">
			<!-- #ifdef APP-PLUS -->
				<!-- <view class="appAPPTop" :style="{paddingTop: pagePaddingTop+'px'}"></view> -->
			<!-- #endif -->
			<view class="icons" @click="back">
				<u-icon name="arrow-left"  ></u-icon>
			</view>
			<view class="title">{{label}}</view>
			<view class="save" @tap="saveEdit(label)">{{saveBtn}}</view>
		</view>
		<view class="target" v-if="label!=='关注'">
			<view class="inputGroup" v-if="label==='运动目的'">
				<u-checkbox-group @change="checkboxGroupChange">
					<u-checkbox
						@change="checkboxChange" 
						v-model="item.checked" 
						shape="circle"
						active-color="#F4333C"
						v-for="(item, index) in spotsList" :key="index" 
						:name="item.id"
					>{{item.name}}</u-checkbox>
				</u-checkbox-group>
				<!-- <u-button @click="checkedAll">全选</u-button> -->
			</view>
			<u-input v-else :placeholder="placeholder" clearable v-model="inputVal" :type="label=='昵称'?type='text':type='number'" :border="border" />
		</view>
		<!-- 关注 -->
		<!-- <view class="focus" v-if="label=='关注'">
			<view class="focus-items" v-for="(item,index) in focusList" :key="index">
				<image class="img" :src="item.headerImg" mode=""></image>
				<view class="info">
					<view class="name">{{item.name}}</view>
					<view class="address">{{item.address}}</view>
				</view>
				<view class="focus-btn">
					{{item.isFocus}}
				</view>
			</view>
		</view> -->
	</view>
</template>

<script>
	import {API} from '../../../../config/myApi.js';
	export default {
		data() {
			return {
				label: '',
				inputVal: '',
				type: '',
				border: false,
				placeholder: '',
				saveBtn: '保存',
				defaultChosse: [],
				selectChoose: '',
 				spotsList: [],
				focusList: [
					{
						headerImg: '../../../../static/img/header.jpg',
						name: '胡晓静',
						address: '班德尔城558号',
						isFocus: '已关注'
					},
					{
						headerImg: '../../../../static/img/header.jpg',
						name: '胡晓静',
						address: '班德尔城558号',
						isFocus: '已关注'
					},
					{
						headerImg: '../../../../static/img/header.jpg',
						name: '胡晓静',
						address: '班德尔城558号',
						isFocus: '已关注'
					}
				]
			}
		},
		onLoad(option) {
			// console.log('000++00',option)
			this.label = option.label
			// this.label = '关注'
			if(this.label === '关注') {
				this.saveBtn = ' ';
				
			}else if(this.label === '运动目的') {
				this.defaultChosse = option.val.split(',')
				this.getSportsList(this.defaultChosse);
			}
			this.placeholder = `请修改${this.label}`
			this.inputVal = option.val
			console.log('onload',option,this.defaultChosse)
		},
		methods: {
			// 保存
			saveEdit(lab) {
				let pram= {};
				if(lab=='昵称') {
					pram = {};
					pram.nickName = this.inputVal;
				}
				if(lab=='身高') {
					pram = {}
					pram.height = this.inputVal;
				}
				if(lab=='体重') {
					pram = {}
					pram.weight = this.inputVal;
				}
				if(lab=='运动目的') {
					pram.targetType = this.selectChoose;
					
				}
				API.editOwn(pram).then(res=> {
					console.log('editOwn',res)
					if(pram.weight>999) {
						this.$u.toast('体重不合理');
					}
					if(pram.height>400) {
						this.$u.toast('身高不合理');
					}
					if(res.code==200) {
						this.$u.toast(res.message);
						setTimeout(()=>{
							this.back();
						},1000)
					}
				})
				// address, birthday, face, nickName, region, regionId, sex
		        
			},
			
			getfindIdol(lab) {
				const pram = {
					pageNumber: 1,
					pageSize: 10,
				}
				API.findIdol().then(res=>{
					
				})
			},
			// 运动
			getSportsList(val) {
				const pram = {
					name: "",
					page: {
					    order: "",
					    pageNumber: 1,
					    pageSize: 9,
					    sort: ""
					},
					typeIds: [1000]
				};
				API.getDataList(pram).then(res => {
					// console.log('ddddd', res)
					if(res.code==200) {
						const data = res.result.records;
						let list = [];
						for (let item of data) {
							if(item.id.length <5) {
								let index = data.indexOf(item.id)
								list.push(...data.splice(index,1));
							}
						}
						if(val) {
							for (let item of list) {
								for (let vitem of val) {
									if(item.id == vitem) {
										item.checked = true;
									}
								}
							}
						}
						// list[1].checked = true;
						this.spotsList = list;
						console.log('datat', list)
					}
				})
			},
			back() {
				if(this.label === '关注') {
					uni.switchTab({
						url: '/pages/tabbar/user/my'
					})
				} else {
					uni.navigateTo({
						url: '/pages/tabbar/user/personalCenter/personalInformation'
					})
				}
			},
			// 选中某个复选框时，由checkbox时触发
			checkboxChange(e) {
				//console.log(e);
			},
			// 选中任一checkbox时，由checkbox-group触发
			checkboxGroupChange(e) {
				this.selectChoose = e.join(',')
				console.log('ddd',e,this.selectChoose);
			},
			// 全选
			// checkedAll() {
			// 	this.list.map(val => {
			// 		val.checked = true;
			// 	})
			// }
			getFocusList() {
				// idolId: 专家或者关注的人的id, type: 偶像类型, 0专家,1用户 
				API.saveIdol().then(res=> {
					
				})
			},
		}
	}
</script>

<style lang="scss">
	@import '../../aless/personalInformation.scss';
</style>